$(function() {

	/*动态获取html 字体*/
	setFontSize();

	/*1111111111111111111111111111111*/
	var url = location.search;
	var token = localStorage.phone;
	var select_type = url.split('=')[1];
	var title = 0;
	var falseAnswer = 0;
	var a = 0; //获取用户做到第几题了  $.ajax
	var titleID = a;
	var dui = 0;
	var cuo = 0;
	var id = Array();
	var cid = Array();
	var b;
getInfo();
	function back() {
		var type = 'get_continue_id';
		var id = titleID;
		if(select_type == 'shunxu' || select_type == 'continue') {
			$.ajax({
				url: "https://newapp.icandrive.cn/api/title_select.php",
				data: {
					token: token,
					type: type,
					title_id: id
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {
					if(data.status == 200) {
						//                    alert(id);
						history.back(-1);
						localStorage.removeItem('t');

					}
				},
				error: function() {
					layer.msg('系统繁忙');
				}

			})

		} else {
			history.back(-1);
		}
	}
	/*22222222222222222222222222222222222*/
	if(select_type == 'suiji') {
		$('#collection span').html('收藏');
		$('.name').html('随机练习')
		getInfo();
	} else if(select_type == 'shunxu') {
		$('#collection span').html('收藏');
		$('.name').html('顺序练习')
		getInfo();
	} else if(select_type == 'showColl') {
		$('.name').html('我的收藏');
		$('#collection span').html('取消收藏');
		getInfo();
	} else if(select_type == 'wrong') {
		$('#collection span').html('收藏');
		$('.name').html('我的错题');
		getInfo();
	} else if(select_type == 'easy_wrong') {
		$('#collection span').html('收藏');
		$('.name').html('易错题');
		getInfo();
	} else if(select_type == 'continue') {
		$('#collection span').html('收藏');
		$('.name').html('未做习题');
		if(select_type == 'continue') {
			var type = 'get_continue';
			$.ajax({
				url: "https://newapp.icandrive.cn/api/demo.php",
				data: {
					token: token,
					typeT: type,
					page: 5
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {
					if(data.status == 200) {
						var p = data.list.continue_id;
//						a = parseInt(p);
						a=p;
						titleID = a;
						getInfo();
					}

				},
				error: function() {
					layer.msg('系统繁忙');
				}
			})
		}
	}

	/*3333333333333333333333333333333333*/
	if(select_type == 'showColl') {
		var type = 'cancel_coll';
		$('.coll').on('click', function() {
			var id = $('#collection span').attr('title_id');
			$.ajax({
				url: "https://newapp.icandrive.cn/api/title_select.php",
				data: {
					token: token,
					type: type,
					title_id: id
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {
					if(data.status == 200) {
						layer.msg('取消收藏成功');
					}
				},
				error: function() {
					layer.msg('系统繁忙');
				}
			})
		})
	} else {
		$('.coll').on('click', function() {
			var id = $('#collection span').attr('title_id');
			var type = 'collection';
			$.ajax({
				url: "https://newapp.icandrive.cn/api/title_select.php",
				data: {
					token: token,
					type: type,
					title_id: id
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {
					if(data.status == 200) {
						layer.msg('收藏成功');
					} else if(data.status == 300) {
						layer.msg('已经收藏过了');
					}
				},
				error: function() {
					layer.msg('系统繁忙');
				}
			})
		})
	}
	//下一题
	$(".main").on('click', '.next', function() {
		var id = $(this).parent().next().find('.next').attr('id');
		$('#collection span').attr('title_id', id);
		$('#collection span').html('收藏');
		if(select_type == 'shunxu' || select_type == 'continue') {
			titleID++;
		}
		var count = $(this).parent().parent().find('.t').length;
		var index = $(this).parent().index() + 1;
		$(this).parent().hide();
		$(this).parent().next().show();
		$('.index').html(index);
		$('.last i').html(count);
		if(select_type == 'showColl') {
			$('#collection span').html('取消收藏');
		} else {
			$('#collection span').html('收藏');
		}
		if(select_type == 'shunxu' || select_type == 'suiji' || select_type == 'continue') {
			if((count - index) == 3) {
				a = a + 10;
				$.ajax({
					url: "https://newapp.icandrive.cn/api/demo.php",
					type: 'post',
					dataType: 'json',
					data: {
						page: a,
						typeT: select_type,
						token: token
					},
					success: function(data) {
						if(data.status == 200) {
							console.log(data);
							b = a + 1;
							$(data.list).each(function() {
								var wrongrate = (Math.round(this.wrongrate * 100)) + '%';
								var imgTemp = '';
								if(this.img != null && this.img != '') {
									imgTemp = "<div class='print'><img src='../img/" + this.img + "'></div>";
								}
								var temp = '';
								if($.trim(this.type) != 'Type:0') {
									var title_type = '选择题';

									temp = "<li><span class='but'>A</span><span>" + this.a + "</span></li><li><span class='but'>B</span><span>" + this.b + "</span></li><li><span class='but'>C</span><span>" + this.c + "</span></li><li><span class='but'>D</span><span>" + this.d + "</span></li>";
								} else {
									var title_type = '判断题';
									temp = "<li><span class='but'>A</span><span>" + this.a + "</span></li><li><span class='but'>B</span><span>" + this.b + "</span></li>";
								}
								$('.main').append("<div style='display: none' class='t main_wrap main_wrap1'><a class='select'><p>" + title_type + "</p><p><span>收藏</span><span>233</span></p><p><span>错误率</span><span>" + wrongrate + "</span></p></a><div class='topic'>" + '<span> ' + b + '、' + this.title + "</span>" + imgTemp + "</div><div class='key'><ul>" + temp + "</ul><button class='sure' id='" + this.id + "'>确定</button></div><div class='analysis' style='display: none'><span>题目解析：<i class='answer'>" + this.answer + "</i></span><p>" + this.content + "</p></div><button id='" + this.id + "' class='next' style='display: none'>下一题</button></div>")
								b++;
							});
						}
						$('.main .t').hide();
						$('.main>div').eq(index - 1).show();
						$("#collection span").attr('title_id', $('.main>div').eq(0).find('.next').prop('id'));
					}
				})
			}
		}
		if(count + 1 - index == 0) {
			layer.msg('最后一题了')
		}
	})
	//选题

	$('.main').on('click', '.but', function() {
		if($(this).css('backgroundColor') != 'rgba(0, 0, 0, 0)') {
			$(this).css({
				backgroundColor: 'rgba(0, 0, 0, 0)',
				color: 'black'
			});
		} else {
			$(this).css({
				backgroundColor: 'green',
				color: 'white'
			});

		}

	});
	$('.main').on('click', '.sure', function() {
		var str = '';
		$(this).parent().find('.but').each(function() {
			console.log($(this).css('backgroundColor'))
			if($(this).css('backgroundColor') != 'rgba(0, 0, 0, 0)') {
				str += $(this).text();
			}
		});

		if(str == '') {
			return false;
		}
		$(this).parent().find('.but').removeClass('but');
		title++;
		var answer = $(this).parent().next().children().find('.answer').text().toUpperCase();
		answer = answer.replace(/\s/g, "")
		if(str != answer) {
			$(this).parent().next().append("<span style='color: red'>×</span>");
			falseAnswer++;
			id.push($(this).parent().parent().attr('id')); //題目id
			//            alert(id);
		} else {
			$(this).parent().next().append("<span style='color: green'>√</span>");
			cid.push($(this).parent().parent().attr('id')); //題目id
			//            alert(cid);
		}
		$(this).hide();
		$(this).parent().next().show(); //显示解析
		$(this).parent().next().next().show(); //显示下一题
		//        $('.but').parent().parent().find('.but').removeClass('but');
	})
	//交卷
	function sub() {
		//        console.log(id);
		$.ajax({
			url: "https://newapp.icandrive.cn/api/title.php",
			data: {
				token: token,
				wrong: id,
				right: cid
			},
			dataType: 'json',
			type: 'post',
			success: function(data) {
				if(data.status == 200) {
					console.log(titleID);
					var index = title - falseAnswer; //错误率 保留两位小数
					layer.msg("本次得分为:" + index * 2, function() {
						history.back();
					}, 2000);
				}
			},
			error: function() {}
		})

	}

	function getInfo() {
		$.ajax({
			url: "https://newapp.icandrive.cn/api/demo.php",
			type: 'post',
			dataType: 'json',
			data: {
				page: a,
				typeT: select_type,
				token: token
			},
			success: function(data) {
				if(data.status == 200) {
					var b = a + 1;
					if(select_type != 'shunxu' && select_type != 'suiji' && select_type != 'continue') {
						var tt = data.list.length;
						$('.count').html(tt);

					}
					$(data.list).each(function() {
						var wrongrate = (Math.round(this.wrongrate * 10000)) / 100 + '%';

						var imgTemp = '';
						if(this.img != null && this.img != '') {
							imgTemp = "<div class='print'><img src='../img/" + this.img + "'></div>";
						}
						var temp = '';
						if($.trim(this.type) != 'Type:0') {
							var title_type = '选择题';

							temp = "<li><span class='but'>A</span><span>" + this.a + "</span></li><li><span class='but'>B</span><span>" + this.b + "</span></li><li><span class='but'>C</span><span>" + this.c + "</span></li><li><span class='but'>D</span><span>" + this.d + "</span></li>";
						} else {
							var title_type = '判断题';
							temp = "<li><span class='but'>A</span><span>" + this.a + "</span></li><li><span class='but'>B</span><span>" + this.b + "</span></li>";
						}
						$('.main').append("<div class='t main_wrap main_wrap1'><a class='select'><p>" + title_type + "</p><p><span>收藏</span><span>233</span></p><p><span>错误率</span><span>" + wrongrate + "</span></p></a><div class='topic'>" + '<span> ' + b + '、' + this.title + "</span>" + imgTemp + "</div><div class='key'><ul>" + temp + "</ul><button class='sure' id='" + this.id + "'>确定</button></div><div class='analysis' style='display: none'><span>题目解析：<i class='answer'>" + this.answer + "</i></span><p>" + this.content + "</p></div><button id='" + this.id + "' class='next' style='display: none'>下一题</button></div>")
						b++;
					});
				}

				$('.main .t').hide();
				$('.main>div').eq(0).show();

				$("#collection span").attr('title_id', $('.main>div').eq(0).find('.next').prop('id'));
			}
		})
	}

}); /*end $function*/

/*动态html 字体*/
var win = window,
	doc = document;

function setFontSize() {
	var winWidth = $(window).width();

	//750这个数字是根据你的设计图的实际大小来的，所以值具体根据设计图的大小
	var size = (winWidth / 980) * 100;
	doc.documentElement.style.fontSize = (size < 100 ? size : 100) + 'px';
}

/*监听手机返回键功能*/
return_keybord();
